﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace My3DGame.Game_Base.Cameras
{
    public class clsCameraBackground : Microsoft.Xna.Framework.GameComponent
    {
        //Camera matrices
        public Matrix view { get; set; }
        public Matrix projection { get; protected set; }
        private Matrix world;

        public Matrix World
        {
            get { return world; }
            set { world = value; }
        }

        // Camera vectors
        public Vector3 cameraPosition { get; protected set; }
        Vector3 cameraDirection;
        public Vector3 cameraUp;
      
        //cac vi tri cua camera
        float cameraX = (float)Math.Cos(1 * 0.33f);
        float cameraY = (float)Math.Sin(1 * 0.33f);
        Vector3 camPos = new Vector3(-50, 0, 20);
        Vector3 cameraTarget;
        float time = 0;

        public Vector3 CameraDirection
        {
            get { return cameraDirection; }
            set { cameraDirection = value; }
        }

        public clsCameraBackground(Game game, Vector3 pos, Vector3 target, Vector3 up)
            : base(game)
        {
            // Build camera view matrix
            cameraPosition = pos;
            cameraDirection = target - pos;
            cameraDirection.Normalize();
            cameraUp = up;
            CreateLookAt();


            projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4,
                (float)Game.Window.ClientBounds.Width /
                (float)Game.Window.ClientBounds.Height,
                1, 3000);
        }

        public override void Initialize()
        {
            // Set mouse position and do initial get state
            Mouse.SetPosition(Game.Window.ClientBounds.Width / 2,
                Game.Window.ClientBounds.Height / 2);
            World = Matrix.CreateTranslation(cameraPosition+new Vector3(0,-10,-120)) * Matrix.CreateScale(0.4f)*Matrix.CreateRotationY(MathHelper.Pi);

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            // Recreate the camera view matrix
            CreateLookAt();

            //keyboard input
            //keyboardInput();
            base.Update(gameTime);
        }

        private void keyboardInput()
        {
            Vector3 cameraFront = new Vector3(-cameraY, 0, cameraX);

            cameraTarget = cameraPosition + cameraFront;
            KeyboardState currentKeyboardState = Keyboard.GetState();
            GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);
            #region check when user press left keyboard
            if (currentKeyboardState.IsKeyDown(Keys.Left))
            {
                Vector3 vtd = new Vector3(-cameraY, 0, cameraX);
                cameraX -= vtd.X * 0.02f;
                cameraY -= vtd.Z * 0.02f;
                time += 0.01f;
            }
            #endregion

            #region check when user press right keyboard
            if (currentKeyboardState.IsKeyDown(Keys.Right))
            {

                Vector3 vtd = new Vector3(-cameraY, 0, cameraX);
                cameraX += vtd.X * 0.02f;
                cameraY += vtd.Z * 0.02f;
                time += 0.01f;
            }
            #endregion

            #region check when user press up keyboard
            if (currentKeyboardState.IsKeyDown(Keys.Up))
            {
                Vector3 vtd = cameraTarget - camPos;
                camPos += vtd * 1.5f;
                cameraTarget += vtd * 1.5f;
                time += 0.01f;
            }
            #endregion

            #region check when user press down keyboard
            if (currentKeyboardState.IsKeyDown(Keys.Down))
            {
                Vector3 vtd = cameraTarget - camPos;
                camPos -= vtd * 1.5f;
                cameraTarget -= vtd * 1.5f;
                time -= 0.01f;
            }
            cameraPosition = camPos;
            cameraDirection = cameraTarget - cameraPosition;
            #endregion
        }

        private void CreateLookAt()
        {
            view = Matrix.CreateLookAt(cameraPosition,
                cameraPosition + cameraDirection, cameraUp);
        }
    }
}
